package cn.wolfcode.crm.util;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.shiro.authz.UnauthorizedException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.method.HandlerMethod;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@ControllerAdvice
public class HandlerExceptionUtil {
    /**
     * 指定的异常在该方法中处理
     */
    @ExceptionHandler(UnauthorizedException.class)
    public void HandlerException (HttpServletResponse response, HandlerMethod method )throws IOException{
        ResponseBody responseBody = method.getMethodAnnotation(ResponseBody.class);
        if(responseBody!=null){
            response.setContentType("application/json;charset=utf-8");
            response.getWriter().print(new ObjectMapper().writeValueAsString(JsonResult.mark("没有该权限进行操作")));
        }else {
            response.sendRedirect("/nopermission.jsp");
        }
    }
}
